<template>
  <div class='tags-container'>
    <el-card shadow="always" :body-style="{ padding: '20px'}">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <div class="topNav">
          <el-row :gutter="20" justify="space-between">
            <el-col :span="5">
              <el-form-item label="标签名称" size="small" style="margin-left:8px">
                <el-input v-model="queryParams.tagName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="17">
              <el-form-item label="状态" size="small">
                <el-select v-model="value" placeholder="请选择">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-button size="small" @click="clearInput">清除</el-button>
              <el-button type="primary" size="small" @click="handleSearch">搜索</el-button>
            </el-col>
            <el-col :span="2">
              <el-form-item size="small">
                <el-button type="success" icon="el-icon-edit" style="margin-left:3px" @click="handleAddTags">新增标签</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <template>
        <el-alert :closable="false" type="info" size='small' show-icon class="alert">数据一共 {{total}} 条
        </el-alert>
      </template>
      <template>
        <el-table :data="list" style="width: 100%" v-loading='loading'>
          <el-table-column fixed label="序号" width="80" type="index">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="240">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称" width="240">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="240">
          </el-table-column>
          <el-table-column prop="addDate" label="创建日期" width="240">
            <template #default='{row}'>
              {{$dateFormat(row.addDate)}}
            </template>
          </el-table-column>
          <el-table-column label="状态" width="160">
            <template #default='{row}'>
              {{row.state===1?'已启用':'已禁用'}}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="170">
            <template #default='{row}'>
              <el-button type="text" size="medium" @click="handleIsOn(row)">{{row.state===1?'禁用':'启用'}}</el-button>
              <el-button type="text" size="medium" :disabled='Boolean(row.state)' @click="handleChange(row)">修改</el-button>
              <el-button type="text" size="medium" :disabled='Boolean(row.state)' @click="handleDel(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template>
        <el-pagination class="footer" background :current-page='queryParams.page' :page-size="queryParams.pagesize" :page-sizes="[2, 5, 10, 50]" layout="->, prev, pager, next, sizes, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </template>
    </el-card>
    <TagsAdd :dialogFormVisible.sync='dialogFormVisible' />
    <TagsChange :dialogChangeVisible.sync='dialogChangeVisible' :changeId='changeId' />
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add.vue'
import TagsChange from '../components/tags-change.vue'

export default {
  name: 'tags',
  components: { TagsAdd, TagsChange },
  data () {
    return {
      // 当前页面list中数据长度
      currentListLength: 0,
      loading: false,
      value: null,
      // 点击修改的项目id（需要传给子组件）
      changeId: 0,
      // 控制修改弹窗显示隐藏
      dialogChangeVisible: false,
      // 控制新增弹窗显示隐藏
      dialogFormVisible: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        pagesize: 5,
        tagName: '',
        state: null
      },
      list: []
    }
  },
  methods: {
    async loadTagsList () {
      this.loading = true
      const ret = await list(this.queryParams)
      if (ret.status === 200) {
        this.loading = false
        this.list = ret.data.items
        this.total = ret.data.counts
        this.currentListLength = ret.data.items.length
      }
    },
    // 监听每页条数改变
    handleSizeChange (pagesize) {
      this.queryParams.pagesize = pagesize
      this.loadTagsList()
    },
    // 监听当前页码
    handleCurrentChange (page) {
      this.queryParams.page = page
      this.loadTagsList()
    },
    // 清除顶部导航输入框信息
    clearInput () {
      this.value = null
      this.queryParams = {
        page: this.queryParams.page,
        pagesize: this.queryParams.pagesize,
        tagName: '',
        state: null
      }
    },
    // 点击新增标签按钮
    handleAddTags () {
      this.dialogFormVisible = true
    },
    async handleDel (obj) {
      const ret = await this.$confirm('是否删除此标签？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (ret === 'cancel') return
      try {
        if (this.currentListLength === 1) {
          this.queryParams.page--
        }
        const ret = await remove(obj)
        if (ret.status === 200) {
          this.loadTagsList()
          this.$message.success('标签删除成功')
        }
      } catch {
        this.$message.error('标签删除失败')
      }
    },
    // 点击修改按钮
    handleChange (obj) {
      this.dialogChangeVisible = true
      this.changeId = obj.id
    },
    // 点击搜索按钮
    async handleSearch () {
      if (!this.value && !this.queryParams.tagName) {
        this.loadTagsList()
        this.queryParams.page = 1
      } else {
        this.queryParams.state = this.value
        const ret = await list(this.queryParams)
        if (ret.status === 200) {
          this.list = ret.data.items
          this.total = ret.data.counts
          this.loadTagsList()
          this.queryParams.page = 1
        }
      }
    },
    // 操作启用禁用
    async handleIsOn (obj) {
      obj.state = obj.state === 0 ? 1 : 0
      const ret = await changeState(obj)
      if (ret.status === 200) {
        this.$message.success('修改成功')
      } else {
        this.$message.error('修改失败')
      }
    }
  },
  created () {
    this.loadTagsList()
  }
}
</script>

<style scoped lang='less'>
.tags-container {
  padding: 10px 10px;
  .topNav {
    margin-left: 6px;
  }
  .alert {
    margin-bottom: 16px;
  }
  .footer {
    margin-top: 20px;
  }
}
</style>
